{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "Copy of pytorch_cpp_colab_notebook.ipynb",
      "provenance": [],
      "private_outputs": true,
      "collapsed_sections": [
        "VbwYTgxWvfMD"
      ],
      "toc_visible": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "accelerator": "GPU"
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "VS2lCrJC55PX",
        "colab_type": "text"
      },
      "source": [
        "\n",
        "![Pytorch Cpp](https://github.com/prabhuomkar/pytorch-cpp/raw/master/images/pytorch_cpp.png)\n",
        "\n",
        "This notebook is intended to be used on [Google Colab](https://colab.research.google.com). It allows you\n",
        "to build and run the [pytorch-cpp](https://github.com/prabhuomkar/pytorch-cpp) tutorials on a hosted GPU equipped system for free."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "2giT9_5csfEP",
        "colab_type": "text"
      },
      "source": [
        "#Setup\n",
        "1. Switch to a GPU runtime: `Runtime -> Change runtime type -> Hardware accelerator: Select GPU`\n",
        "2. Run each of the following setup cells one after the other."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "ZTcewDy6cnxQ",
        "colab_type": "text"
      },
      "source": [
        "##Magics and Imports"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "s7-GsT7v-TAh",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# For nicer looking images\n",
        "%matplotlib inline\n",
        "%config InlineBackend.figure_format = 'retina'\n",
        "\n",
        "# Python package imports to display images\n",
        "import matplotlib.pyplot as plt\n",
        "from PIL import Image\n",
        "from pathlib import Path\n",
        "from google.colab import widgets\n",
        "from google.colab import output\n",
        "import matplotlib.pyplot as plt\n",
        "import pandas as pd\n",
        "import os"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "2ASwLgYo3AkT",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Image plotting helper function\n",
        "def plot_images(file_paths, nrows, ncols, figsize_per_image):\n",
        "    fig, ax = plt.subplots(nrows, ncols, figsize=(figsize_per_image[0] * ncols, figsize_per_image[1] * nrows))\n",
        "\n",
        "    n_files = len(file_paths)\n",
        "\n",
        "    if nrows == 1 or ncols == 1:\n",
        "        for i, image_path in enumerate(file_paths):\n",
        "            ax[i].imshow(Image.open(image_path), aspect=\"equal\")\n",
        "            ax[i].axis(\"off\")\n",
        "            ax[i].set_title(image_path.name)\n",
        "    else:\n",
        "        for i in range(nrows):\n",
        "            for j in range(ncols):\n",
        "                ax[i,j].imshow(Image.open(file_paths[i * ncols + j]), aspect=\"equal\")\n",
        "                ax[i,j].axis(\"off\")\n",
        "                ax[i,j].set_title(file_paths[i * ncols + j].name)\n",
        "    fig.tight_layout()\n",
        "    plt.show()\n",
        "\n",
        "# Image captioning helper functions\n",
        "def create_output_datafram(output_path):\n",
        "    output_data = []\n",
        "    for f in output_path.iterdir():\n",
        "        img_name, num, word = f.name.rsplit('_', 2)\n",
        "        word = word.split('.')[0]\n",
        "        output_data.append({\"file\": f, \"image\": img_name, \"num\": int(num), \"word\": word})\n",
        "\n",
        "    df = pd.DataFrame(output_data)\n",
        "\n",
        "    return df.groupby(\"image\").apply(pd.DataFrame.sort_values, 'num')[[\"file\", \"word\"]].groupby(\"image\").agg(list)\n",
        "\n",
        "def create_tab(output_example):\n",
        "    print(f\"Image: {output_example.Index}\")\n",
        "\n",
        "    t = widgets.TabBar(output_example.word)\n",
        "\n",
        "    for i, image_file in enumerate(output_example.file):\n",
        "        with t.output_to(i, select=False):\n",
        "            _, ax = plt.subplots(figsize=(8, 8))\n",
        "            ax.imshow(Image.open(image_file), aspect=\"equal\")\n",
        "            ax.axis(\"off\")\n",
        "            plt.show()\n",
        "\n",
        "def plot_image_caption_output(output_path):\n",
        "    df = create_output_datafram(output_path)\n",
        "    for p, _ in zip(df.itertuples(), widgets.Grid(len(df), 1)):\n",
        "        create_tab(p)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "cs1nFZkH59Nb",
        "colab_type": "text"
      },
      "source": [
        "##Install more recent CMake Version"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "m2Lf4RSjou6t",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "!rm -rf deps\n",
        "!mkdir deps\n",
        "%cd /content/deps\n",
        "!wget --no-check-certificate https://www.cmake.org/files/v3.14/cmake-3.14.0-Linux-x86_64.tar.gz\n",
        "!tar -xvf cmake-3.14.0-Linux-x86_64.tar.gz > /dev/null\n",
        "%mv cmake-3.14.0-Linux-x86_64 cmake-install\n",
        "old_path = %env PATH\n",
        "new_path = \"/content/deps/cmake-install:/content/deps/cmake-install/bin:\" + old_path\n",
        "%env PATH={new_path}\n",
        "%cd /content/\n",
        "!cmake --version"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "ZCawMy0erYiQ",
        "colab_type": "text"
      },
      "source": [
        "##Installed Programs & GPU"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "pMMGmOVmra8B",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "%%bash\n",
        "# Preinstalled compiler versions\n",
        "g++ --version\n",
        "gcc --version\n",
        "# Check installed CMake version\n",
        "cmake --version\n",
        "echo \"\"\n",
        "# Preinstalled pytorch/torchvision versions\n",
        "pip show torch\n",
        "echo \"\"\n",
        "pip show torchvision\n",
        "echo \"\"\n",
        "# GPU Informations\n",
        "nvidia-smi"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "saxZiiBKsjbj",
        "colab_type": "text"
      },
      "source": [
        "##Clone Repo"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "pYr7GTcGsnvx",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "!git clone https://github.com/prabhuomkar/pytorch-cpp.git\n",
        "%cd /content/pytorch-cpp\n",
        "%ls"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Jso4xNoBtRsk",
        "colab_type": "text"
      },
      "source": [
        "##Generate Build System"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "rbeD5lKStWhS",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "%rm -rf build\n",
        "!cmake -B build -D CUDA_V=10.1 -D CMAKE_BUILD_TYPE=Release"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "YOqr6Z6Mt5y9",
        "colab_type": "text"
      },
      "source": [
        "##Build Tutorials"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "H-n_1d4Mt8MG",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "!cmake --build build"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "YRgm8b_yvZyQ",
        "colab_type": "text"
      },
      "source": [
        "#Run Tutorials\n",
        "\n",
        "### 1. Basics\n",
        "* [PyTorch Basics](#scrollTo=OVBcBgknvm_W)\n",
        "* [Linear Regression](#scrollTo=eX6YN9DB04r2)\n",
        "* [Logistic Regression](#scrollTo=3rfrwqKU1VZm)\n",
        "* [Feedforward Neural Network](#scrollTo=ywmM2xkt1mi0)\n",
        "\n",
        "### 2. Intermediate\n",
        "* [Convolutional Neural Network](#scrollTo=2gjZC6b_2PEz)\n",
        "* [Deep Residual Network](#scrollTo=Kla2SaVv228f)\n",
        "* [Recurrent Neural Network](#scrollTo=aTrOcUke3Zxu)\n",
        "* [Bidirectional Recurrent Neural Network](#scrollTo=FVVx0XFY3yvU)\n",
        "* [Language Model (RNN-LM)](#scrollTo=gnvBsukO4H00)\n",
        "\n",
        "### 3. Advanced\n",
        "* [Generative Adversarial Networks](#scrollTo=ggBdsvyR8UMN)\n",
        "* [Variational Auto-Encoder](#scrollTo=CYzbCRPo_TJ7)\n",
        "* [Neural Style Transfer](#scrollTo=ix6R5AosZHTx)\n",
        "* [Image Captioning (CNN-RNN)](#scrollTo=aEBcqiKr-5Sb)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "VbwYTgxWvfMD",
        "colab_type": "text"
      },
      "source": [
        "##Basics"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "U1S-Iruk0GAB",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "%ls /content/pytorch-cpp/build/tutorials/basics/ -1"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "OVBcBgknvm_W",
        "colab_type": "text"
      },
      "source": [
        "### Pytorch-Basics"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Ev7P19zzzRfu",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Create required torchscript module:\n",
        "%cd /content/pytorch-cpp/tutorials/basics/pytorch_basics/models/\n",
        "!python create_resnet18_scriptmodule.py"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Z2pMypzTywYj",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "%ls /content/pytorch-cpp/build/tutorials/basics/pytorch_basics/"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "l4YOWqn2y_0G",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Run\n",
        "%cd /content/pytorch-cpp/build/tutorials/basics/pytorch_basics/\n",
        "!./pytorch-basics"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "eX6YN9DB04r2",
        "colab_type": "text"
      },
      "source": [
        "###Linear Regression"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "BPXFDYqO1DqW",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "%ls /content/pytorch-cpp/build/tutorials/basics/linear_regression/"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "6agvmvVh1J7f",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Run\n",
        "%cd /content/pytorch-cpp/build/tutorials/basics/linear_regression/\n",
        "!./linear-regression"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "3rfrwqKU1VZm",
        "colab_type": "text"
      },
      "source": [
        "###Logistic Regression"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "mIBloL341Yis",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "%ls /content/pytorch-cpp/build/tutorials/basics/logistic_regression/"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "qkKPAWBk1d9V",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Run\n",
        "%cd /content/pytorch-cpp/build/tutorials/basics/logistic_regression/\n",
        "!./logistic-regression"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "ywmM2xkt1mi0",
        "colab_type": "text"
      },
      "source": [
        "###Feedforward Neural Network"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "jZsPU07C1p-K",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "%ls /content/pytorch-cpp/build/tutorials/basics/feedforward_neural_network/"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "DAgpSavJ1tjH",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Run\n",
        "%cd /content/pytorch-cpp/build/tutorials/basics/feedforward_neural_network/\n",
        "!./feedforward-neural-network"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "XFVlcoQKvhX3",
        "colab_type": "text"
      },
      "source": [
        "##Intermediate"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "TVcLYosB16Xi",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "%ls /content/pytorch-cpp/build/tutorials/intermediate/ -1"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "2gjZC6b_2PEz",
        "colab_type": "text"
      },
      "source": [
        "###Convolutional Neural Network"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "-ERzOw4F2ap1",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "%ls /content/pytorch-cpp/build/tutorials/intermediate/convolutional_neural_network/"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "PygE27Dq2mUp",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Run\n",
        "%%time\n",
        "%cd /content/pytorch-cpp/build/tutorials/intermediate/convolutional_neural_network/\n",
        "!./convolutional-neural-network"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Kla2SaVv228f",
        "colab_type": "text"
      },
      "source": [
        "###Deep Residual Network"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "grh7dIl-2y_5",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "%ls /content/pytorch-cpp/build/tutorials/intermediate/deep_residual_network/"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "t6sxgY3U28Fj",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Run\n",
        "%%time\n",
        "%cd /content/pytorch-cpp/build/tutorials/intermediate/deep_residual_network/\n",
        "!./deep-residual-network"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "aTrOcUke3Zxu",
        "colab_type": "text"
      },
      "source": [
        "###Recurrent Neural Network"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "m2C4zWx_3iyM",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "%ls /content/pytorch-cpp/build/tutorials/intermediate/recurrent_neural_network/"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "xqEYhxql3qKr",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Run\n",
        "%%time\n",
        "%cd /content/pytorch-cpp/build/tutorials/intermediate/recurrent_neural_network/\n",
        "!./recurrent-neural-network"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "FVVx0XFY3yvU",
        "colab_type": "text"
      },
      "source": [
        "###Bidirectional Recurrent Neural Network"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "XwKsI8Cc315L",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "%ls /content/pytorch-cpp/build/tutorials/intermediate/bidirectional_recurrent_neural_network/"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "GvZmNxTr34eM",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Run\n",
        "%%time\n",
        "%cd /content/pytorch-cpp/build/tutorials/intermediate/bidirectional_recurrent_neural_network/\n",
        "!./bidirectional-recurrent-neural-network"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "gnvBsukO4H00",
        "colab_type": "text"
      },
      "source": [
        "###Language Model\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "mvKxwskb4K81",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "%ls /content/pytorch-cpp/build/tutorials/intermediate/language_model/"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "RcHpHp3-4OMw",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Run\n",
        "%%time\n",
        "%cd /content/pytorch-cpp/build/tutorials/intermediate/language_model/\n",
        "!./language-model"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "u8sZ3Wk2720U",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Results\n",
        "%cat /content/pytorch-cpp/build/tutorials/intermediate/language_model/output/sample.txt"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "hJS5K-6rvjkW",
        "colab_type": "text"
      },
      "source": [
        "##Advanced"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "xZKTaitM4e9L",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "%ls /content/pytorch-cpp/build/tutorials/advanced/"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "ggBdsvyR8UMN",
        "colab_type": "text"
      },
      "source": [
        "###Generative Adversarial Network"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "t3ehQI_O8bEM",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "%ls /content/pytorch-cpp/build/tutorials/advanced/generative_adversarial_network/"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "xPKI6qSw8n2F",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Run\n",
        "%%time\n",
        "%cd /content/pytorch-cpp/build/tutorials/advanced/generative_adversarial_network/\n",
        "!./generative-adversarial-network"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "CQ7Dt6dr9Hug",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Results\n",
        "%ls /content/pytorch-cpp/build/tutorials/advanced/generative_adversarial_network/output/ -1 -r --sort=time | head -10\n",
        "!echo \"...\"\n",
        "%ls /content/pytorch-cpp/build/tutorials/advanced/generative_adversarial_network/output/ -1 -r --sort=time | tail -10"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "LDZDvBUALJtw",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Show results:\n",
        "# Get paths of output files sorted by creation time:\n",
        "gan_output_file_paths = sorted(list(Path(\"/content/pytorch-cpp/build/tutorials/advanced/generative_adversarial_network/output\").iterdir()), key=os.path.getmtime)\n",
        "# Select some indices to show\n",
        "gan_display_indices = [0, 1, len(gan_output_file_paths) // 2, len(gan_output_file_paths) - 1]\n",
        "gan_file_paths = [gan_output_file_paths[i] for i in gan_display_indices]\n",
        "\n",
        "plot_images(gan_file_paths, nrows=1, ncols=len(gan_file_paths), figsize_per_image=(5, 5))"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "CYzbCRPo_TJ7",
        "colab_type": "text"
      },
      "source": [
        "###Variational Autoencoder"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "qP1x5N6PFLw5",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "%ls /content/pytorch-cpp/build/tutorials/advanced/variational_autoencoder/"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Y2PHWLYVFXR5",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Run\n",
        "%%time\n",
        "%cd /content/pytorch-cpp/build/tutorials/advanced/variational_autoencoder/\n",
        "!./variational-autoencoder"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "szHaGKCfFsju",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Results\n",
        "%ls /content/pytorch-cpp/build/tutorials/advanced/variational_autoencoder/output/ -1 -r --sort=time | head -10\n",
        "!echo \"...\"\n",
        "%ls /content/pytorch-cpp/build/tutorials/advanced/variational_autoencoder/output/ -1 -r --sort=time | tail -10"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "elkk38xaGJLU",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "vae_output_file_paths = sorted(list(Path(\"./output\").iterdir()), key=os.path.getmtime)\n",
        "vae_display_indices = [1, 0, len(vae_output_file_paths) // 2, len(vae_output_file_paths) // 2 - 1, len(vae_output_file_paths) - 1, len(vae_output_file_paths) - 2]\n",
        "vae_file_paths = [vae_output_file_paths[i] for i in vae_display_indices]\n",
        "\n",
        "plot_images(vae_file_paths, nrows=len(vae_file_paths) // 2, ncols=2, figsize_per_image=(9, 5))"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "ix6R5AosZHTx",
        "colab_type": "text"
      },
      "source": [
        "###Neural Style Transfer"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "IaEqJbsXZjBD",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Create required torchscript module:\n",
        "%cd /content/pytorch-cpp/tutorials/advanced/neural_style_transfer/model/\n",
        "!python create_vgg19_layers_scriptmodule.py"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "x-oKRmdZZSbz",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "%ls /content/pytorch-cpp/build/tutorials/advanced/neural_style_transfer/"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "bPrnqcFvZXJU",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Run\n",
        "%%time\n",
        "%cd /content/pytorch-cpp/build/tutorials/advanced/neural_style_transfer/\n",
        "!./neural-style-transfer"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Eo8v_9s9eUvR",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Results\n",
        "%ls /content/pytorch-cpp/build/tutorials/advanced/neural_style_transfer/output/ -1 -r --sort=time"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "7RW9fPEyfus8",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Inputs\n",
        "%ls /content/pytorch-cpp/data/neural_style_transfer_images/"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "vSJXkbn2hcgK",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "nst_input_file_paths = sorted(list(Path(\"/content/pytorch-cpp/data/neural_style_transfer_images\").iterdir()))\n",
        "\n",
        "plot_images(nst_input_file_paths, nrows=1, ncols=len(nst_input_file_paths), figsize_per_image=(8, 5))"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Qhyn-FrkekZa",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "nst_output_file_paths = sorted(list(Path(\"/content/pytorch-cpp/build/tutorials/advanced/neural_style_transfer/output\").iterdir()), key=os.path.getmtime)\n",
        "\n",
        "plot_images(nst_output_file_paths, nrows=len(nst_output_file_paths), ncols=1, figsize_per_image=(7, 5))"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "aEBcqiKr-5Sb",
        "colab_type": "text"
      },
      "source": [
        "###Image Captioning"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "s-sgAqiJiGEC",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Create required torchscript module:\n",
        "%cd /content/pytorch-cpp/tutorials/advanced/image_captioning/model/\n",
        "!python create_encoder_cnn_backbone_scriptmodule.py"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "VltI7w1tAuJt",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "%ls /content/pytorch-cpp/build/tutorials/advanced/image_captioning/"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "95UQmwSSBA4X",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Show command line arguments:\n",
        "%cd /content/pytorch-cpp/build/tutorials/advanced/image_captioning/\n",
        "!./image-captioning --help"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "APa5ygazA1aw",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Run\n",
        "%%time\n",
        "%cd /content/pytorch-cpp/build/tutorials/advanced/image_captioning/\n",
        "!./image-captioning --batch_size=128 --num_epochs=4"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Do8Q7qzxBINd",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Results\n",
        "plot_image_caption_output(Path(\"/content/pytorch-cpp/build/tutorials/advanced/image_captioning/output\"))"
      ],
      "execution_count": null,
      "outputs": []
    }
  ]
}